<?php

class pipeijiluModule extends Model
{
    public function __construct()
    {
        parent::__construct("pipeijilu");
    }

    /**
     * 添加匹配结果，并开始匹配
     * @date: 2019年6月19日 下午5:28:12
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function insertone($yonghu, $qiye, $daanJson)
    {
        //相关的匹配数据
        $zhengceshuliang = 0;
        $fuchiedu = 0;
        $pipeijieguo = $this->common->getOrderId();

        $bianhao = $this->common->getOrderId();
        $data["yonghu"] = $yonghu;
        $data["qiye"] = $qiye;
        $data["datijilu"] = $bianhao;
        $data["dtime"] = date("Y-m-d H:i:s");

        //执行匹配操作
        $timuxuanxiangM = new timuxuanxiangModule();
        //查询用户的答案选项
        $timuxuanxiangIds = $timuxuanxiangM->getIdsByJsonstr($daanJson);
        //查询选项的关键词数组==>用户当前的选择模型
        $guanjianciArray = $timuxuanxiangM->getKeywordsByids($timuxuanxiangIds);
        //循环政策库，匹配相识度
        $zhengcekuM = new zhengcekuModule();
        $pipeizhengceM = new Model("pipeizhengce");
        $zhengcekuList = $zhengcekuM->getredislist("");

        foreach ($zhengcekuList as $k => $v) {
            $zhengceku_guanjianci = $v["guanjianci"];
            if (count($guanjianciArray) == 0) {
                returnFail("选择项关键词为空，请检查选项ID是否传递");
            }
            $similarityNum = similarity($guanjianciArray, $zhengceku_guanjianci) + 0;
            $similarityNum = round($similarityNum, 2);
            $v["xiangsidu"] = $similarityNum;

            //过滤相似度较低的政策
            if ($similarityNum > 0.01 && $this->common->isNumber($similarityNum * 100)) {
                //给匹配度加上一个固定值，让匹配率看上去更漂亮 （～_～）
                $similarityNum = $similarityNum + ((1 - $similarityNum) / 4) * 3;
            } else {
                continue;
            }

            //记录当前政策匹配结果
            $pipeizhengceData["laiyuanbianhao"] = $pipeijieguo;
            $pipeizhengceData["zhengce"] = $v["id"];
            $pipeizhengceData["xiangsidu"] = round($similarityNum, 2);
            $pipeizhengceM->insert($pipeizhengceData);
            //增加相关的匹配数据
            $zhengceshuliang++;
            $fuchiedu = $fuchiedu + $v["zuigaojine"];

            //最多匹配30条数据
            if ($zhengceshuliang >= 25) {
                break;
            }
        }
        $data["zhengceshuliang"] = $zhengceshuliang;
        $data["fuchiedu"] = $fuchiedu;
        $data["pipeijieguo"] = $pipeijieguo;
        $pipei_id = $this->insert($data);
        $datijiluM = new datijiluModule();
        $datijiluM->insertlist($yonghu, $bianhao, $daanJson);
        return $pipei_id;
    }

    /**
     * 查详情
     * @date: 2019年6月19日 下午5:46:01
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getdetailByid($id)
    {
        $detail = $this->find("zhengceshuliang,fuchiedu,pipeijieguo")->findOne($id);
        $zhengcekuM = new zhengcekuModule();
        $pipeijieguo = $detail["pipeijieguo"];
        $pipeizhengceM = new model("pipeizhengce");
        $pipeizhengceList = $pipeizhengceM->findBySql("select * ,(SELECT fawenriqi from sl_zhengceku where id=pp.zhengce) as fawenriqi from sl_pipeizhengce as pp  where laiyuanbianhao='{$pipeijieguo}'  order by xiangsidu desc, fawenriqi desc");

        //定义申请中列表
        $shengqingzhonglist = [];
        $notshengqingzhonglist = [];
        foreach ($pipeizhengceList as $k => $v) {
            //查询政策列表详细
            $zhengcekuItem = $zhengcekuM->getonelistByid($v["zhengce"]);
            $zhengcekuItem["xiangsidu"] = $v["xiangsidu"] * 100 . "%";
            $zhengcekuItem["shifouyouyong"] = $v["shifouyouyong"];
            $zhengcekuItem["pipeizhengceid"] = $v["id"];
            if ($zhengcekuItem["zhuangtai"] == "申报中") {
                $shengqingzhonglist[] = $zhengcekuItem;
            } else {
                $notshengqingzhonglist[] = $zhengcekuItem;
            }
        }
        $detail["zhengcelist"] = array_merge_recursive($shengqingzhonglist, $notshengqingzhonglist);
        unset($detail["pipeijieguo"]);
        return $detail;
    }

    /**
     * 查询用户的匹配记录列表
     * @date: 2019年6月21日 上午10:17:47
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getlistByyonghu($yonghu)
    {
        $list = $this
            ->find("id,qiye,dtime,fuchiedu,zhengceshuliang")
            ->where("yonghu ='{$yonghu}' ")
            ->all();
        $qiyekuM = new qiyekuModule();
        foreach ($list as $k => $v) {
            $v["qiye"] = $qiyekuM->findOne($v["qiye"])["entname"];
            $v["dtime"] = date("Y-m-d", strtotime($v["dtime"]));
            $list[$k] = $v;
        }
        return $list;
    }

}